package com.lht.admin.service.impl;

import com.lht.admin.dao.IndexDao;
import com.lht.admin.service.IndexService;
import com.lht.admin.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @描述:
 * @创建人: JaccePon
 * @创建日期: 2020年02月08日 22:21
 */
@Slf4j
@Service("indexService")
public class IndexServiceImpl implements IndexService {
    @Autowired
    private IndexDao indexDao;

    /**
     * 统计订单情况
     *
     * @return
     */
    @Override
    public HashMap<String, String> orderCount() {

        HashMap<String, String> result = new HashMap<>();

        HashMap<String, String> params = new HashMap<>();
        //今日订单量统计
        params.put("startTime", DateUtils.format(new Date()));
        params.put("endTime", DateUtils.format(DateUtils.plusDays(new Date(), 1)));
        int todayOrder = indexDao.countOrderToday(params);

        //月订单量统计
        params.put("startTime", DateUtils.format(new Date(), "yyyy-MM"));
        params.put("endTime", DateUtils.format(DateUtils.plusMonths(new Date(), 1), "yyyy-MM"));
        int monthsOrder = indexDao.countOrderToday(params);

        //月贷款总金额
        BigDecimal monthsQouta = indexDao.sumOrderQouta(params);

        //总的贷款金额
        params.clear();
        try {
            BigDecimal allQouta = indexDao.sumOrderQouta(params);

            result.put("todayOrder", todayOrder + "");
            result.put("monthsOrder", monthsOrder + "");
            result.put("monthsQouta", monthsQouta == null ? "0" : monthsQouta.intValue() + "");
            result.put("allQouta", allQouta == null ? "0" : allQouta.intValue() + "");
        } catch (Exception e) {
            log.error("orderCount", e);
        }

        return result;
    }

    /**
     * 热门产品统计
     *
     * @return
     */
    @Override
    public List<HashMap<String, Object>> productCount() {

        List<HashMap<String, Object>> list = indexDao.productCount();

        return list;
    }

    /**
     * 订单进度统计
     *
     * @return
     */
    @Override
    public List<HashMap<String, Object>> processCount() {

        List<HashMap<String, Object>> list = indexDao.processCount();

        double count = Double.valueOf(indexDao.allOrderNum());

        if (list.size() > 0 && count > 0) {

            for (HashMap<String, Object> map : list) {
                Object num = map.get("num");
                String format = String.format("%.2f", Integer.valueOf(num + "") / count);
                map.put("proNum", format);
            }
        }

        return list;
    }

    /**
     * 获取每日统计
     *
     * @param days
     * @return
     */
    @Override
    public List<Integer> latestDaysCount(int days) {
        ArrayList<Integer> list = new ArrayList<>();
        if (days == 0) {
            return list;
        }
        HashMap<String, String> params = new HashMap<>();
        for (int i = 0; i < days; i++) {
            Date dayfirst;
            if (i == days - 1) {
                dayfirst = new Date();
            } else {
                dayfirst = DateUtils.plusDays(new Date(), (i + 1 - days));
            }
            Date secondDay = DateUtils.plusDays(dayfirst, 1);
            params.put("startTime", DateUtils.format(dayfirst));
            params.put("endTime", DateUtils.format(secondDay));
            int todayOrder = indexDao.countOrderToday(params);
            list.add(todayOrder);
        }
        return list;
    }

    @Override
    public Map<String, Map<String, Object>> halfYearCount() {

        Map<String, Map<String, Object>> result = new HashMap<>();

        //获取最近半年
        Date date = new Date();
        String one = DateUtils.format(date, "yyyy-MM");
        String two = DateUtils.format(DateUtils.plusMonths(date, -1), "yyyy-MM");
        String three = DateUtils.format(DateUtils.plusMonths(date, -2), "yyyy-MM");
        String four = DateUtils.format(DateUtils.plusMonths(date, -3), "yyyy-MM");
        String five = DateUtils.format(DateUtils.plusMonths(date, -4), "yyyy-MM");
        String six = DateUtils.format(DateUtils.plusMonths(date, -5), "yyyy-MM");


        HashMap<String, String> params = new HashMap<>();
        params.put("monthStr", one);
        List<Map<String, Object>> oneData = indexDao.orderMonthCount(one);
        params.put("monthStr", two);
        List<Map<String, Object>> twoData = indexDao.orderMonthCount(two);
        params.put("monthStr", three);
        List<Map<String, Object>> threeData = indexDao.orderMonthCount(three);
        params.put("monthStr", four);
        List<Map<String, Object>> fourData = indexDao.orderMonthCount(four);
        params.put("monthStr", five);
        List<Map<String, Object>> fiveData = indexDao.orderMonthCount(five);
        params.put("monthStr", six);
        List<Map<String, Object>> sixData = indexDao.orderMonthCount(six);

        ArrayList<String> title = new ArrayList<>();

        HashMap<String, Object> compera = new HashMap<>();

        HashMap<String, Object> all = new HashMap<>();

        countForHalfYearOrderQouta(title, six, sixData, compera, all);
        countForHalfYearOrderQouta(title, five, fiveData, compera, all);
        countForHalfYearOrderQouta(title, four, fourData, compera, all);
        countForHalfYearOrderQouta(title, three, threeData, compera, all);
        countForHalfYearOrderQouta(title, two, twoData, compera, all);
        countForHalfYearOrderQouta(title, one, oneData, compera, all);

        HashMap<String, Object> titleMap = new HashMap<>();
        titleMap.put("title", title);
        result.put("userMap", compera);
        result.put("all", all);
        result.put("title", titleMap);

        return result;
    }

    private void countForHalfYearOrderQouta(ArrayList<String> title, String six, List<Map<String, Object>> sixData, HashMap<String, Object> compera, HashMap<String, Object> all) {
        title.add(six);
        BigDecimal allQouta = new BigDecimal(0);
        if (sixData != null && sixData.size() > 0) {
            for (Map<String, Object> map : sixData) {
                BigDecimal quota = (BigDecimal) map.get("quota");
                String userName = map.get("userName") + "";
                if (compera.containsKey(userName)) {
                    Map<String, Object> returnMap = (Map<String, Object>) compera.get(userName);
                    HashMap<String, BigDecimal> qoutaMap = (HashMap<String, BigDecimal>) returnMap.get("qoutaMap");
                    qoutaMap.put(six, quota);
                } else {
                    HashMap<String, Object> returnMap = new HashMap<>();
                    HashMap<String, BigDecimal> qoutaMap = new HashMap<>();
                    returnMap.put("name", userName);
                    qoutaMap.put(six, quota);
                    returnMap.put("qoutaMap", qoutaMap);
                    compera.put(userName, returnMap);

                }

                allQouta = quota.add(allQouta);

            }
        }
        all.put(six, allQouta);
    }
}
